package com.zc.app.shiro.dao;

import com.zc.app.shiro.peer.PermissionPeer;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * Created by zc on 2016/7/15.
 */
public class PermissionDaoImpl extends JdbcDaoSupport implements PermissionDao {

//    private JdbcTemplate jdbcTemplate = JdbcTemplateUtils.jdbcTemplate();


    @Override
    public PermissionPeer createPermission(final PermissionPeer permission) {
        final String sql = "INSERT INTO SYS_PERMISSIONS(ID,PERMISSION, DESCRIPTION, AVAILABLE) " +
                "VALUES (NEXTVAL('SEQ_SYS_PERMISSIONS'),?, ?, ?)";
        GeneratedKeyHolder keyHolder = new GeneratedKeyHolder();
        getJdbcTemplate().update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement ps = connection.prepareStatement(sql, new String[]{"id"});
                ps.setString(1, permission.getPermission());
                ps.setString(2, permission.getDescription());
                ps.setBoolean(3, permission.getAvailable());
                return ps;
            }
        }, keyHolder);

        permission.setId(keyHolder.getKey().longValue());
        return permission;
    }

    @Override
    public void deletePermission(Long permissionId) {
        String sql = "DELETE FROM SYS_ROLES_PERMISSIONS WHERE PERMISSION_ID = ? ";
//        getJdbcTemplate().update(sql, permissionId);

        sql = "DELETE FROM SYS_PERMISSIONS WHERE ID = ? ";
//        getJdbcTemplate().update(sql, permissionId);
    }
}
